package com.zengjianjun.es.demo.highlevelclient.param;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;

import java.util.List;

/**
 * @author zengjianjun
 */
public class ParamQueryBuilder {

    public static void paramBoolQueryBuilder(BoolQueryBuilder queryBuilder, EsQueryParam esQueryParam) {
        if (queryBuilder == null || esQueryParam == null) {
            return;
        }
        esQueryParam.forEach((k, v) -> {
            if (StringUtils.isNotBlank(k) && v != null && v.getValue() != null) {
                if (v.getValue() instanceof String) {
                    if (StringUtils.isBlank(String.valueOf(v.getValue()))) {
                        return;
                    }
                }
                switch (v.getConditionOption()) {
                    case prefix:
                        if (v.getValue() instanceof List) {
                            ((List<?>) v.getValue()).forEach(e -> {
                                if (e instanceof String) {
                                    queryBuilder.filter(QueryBuilders.prefixQuery(k, e.toString()));
                                }
                            });
                        } else {
                            if (v.getValue() instanceof String) {
                                queryBuilder.filter(QueryBuilders.prefixQuery(k, v.getValue().toString()));
                            }
                        }
                        break;
                    case equal:
                        if (v.getValue() instanceof List) {
                            ((List<?>) v.getValue()).forEach(e -> {
                                queryBuilder.must(QueryBuilders.termQuery(k, e));
                            });
                        } else {
                            queryBuilder.must(QueryBuilders.termQuery(k, v.getValue()));
                        }
                        break;
                    case not_equal:
                        if (v.getValue() instanceof List) {
                            ((List<?>) v.getValue()).forEach(e -> {
                                queryBuilder.mustNot(QueryBuilders.termQuery(k, e));
                            });
                        } else {
                            queryBuilder.mustNot(QueryBuilders.termQuery(k, v.getValue()));
                        }
                        break;
                    case or:
                        if (v.getValue() instanceof List) {
                            ((List<?>) v.getValue()).forEach(e -> {
                                queryBuilder.should(QueryBuilders.termQuery(k, e));
                            });
                        } else {
                            queryBuilder.should(QueryBuilders.termQuery(k, v.getValue()));
                        }
                        queryBuilder.minimumShouldMatch(1);
                        break;
                    case like:
                        if (v.getValue() instanceof List) {
                            ((List<?>) v.getValue()).forEach(e -> {
                                queryBuilder.filter(QueryBuilders.wildcardQuery(k, e.toString()));
                            });
                        } else {
                            queryBuilder.filter(QueryBuilders.wildcardQuery(k, v.getValue().toString()));
                        }
                        break;
                    default:
                        break;
                }
            }
        });
        if (esQueryParam.getTimeOption() != null) {
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(esQueryParam.getTimeOption().getField());
            rangeQueryBuilder.gte(esQueryParam.getTimeOption().getStartTime());
            rangeQueryBuilder.lte(esQueryParam.getTimeOption().getEndTime());
            queryBuilder.must(rangeQueryBuilder);
        }
    }
}
